package com.xy.bizportdemo;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;

import com.squareup.leakcanary.LeakCanary;
import com.xy.bizportdemo.controller.SmartSMSManager;
import com.xy.bizportdemo.database.SmsDBManager;
import com.xy.bizportdemo.util.StrictModeUtil;

import hugo.weaving.DebugLog;

public class MyApplication extends Application {
    private static final String TAG = "MyApplication";
    private static MyApplication mContext = null;

    private Handler mHandler;

    public static MyApplication getMyApplication() {
        return mContext;
    }

    private void initHandler() {
        HandlerThread thread = new HandlerThread("database");
        thread.start();
        mHandler = new Handler(thread.getLooper());
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;

        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        } else {
            LeakCanary.install(this);
            ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            // Normal app init code...
            int memoryClass = am.getMemoryClass();
            Log.d(TAG, "onCreate: total memory=" + memoryClass);
        }

        initStrictMode();
        initHandler();
        mHandler.post(new Runnable() {
            @DebugLog
            @Override
            public void run() {
                initDB();
                initXySDK();
                initLocation();
            }
        });
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    private void initStrictMode() {
        StrictModeUtil.init(mContext);//StrictMode性能初始化设置
        this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                StrictModeUtil.open(mContext);//StrictMode性能检测
            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
    }

    /**
     * 初始化小源SDK
     */
    private void initXySDK() {
        SmartSMSManager.getInstance(this).init();
//        com.xy.smartsms.util.Log.level = com.xy.smartsms.util.Log.DEBUG;
//
//        SharedPreferences sharedPreferences = getSharedPreferences(BizportContentUtil.SETTING_FILE_NAME,
//                Context.CONTEXT_IGNORE_SECURITY);
//        String onlineUpdateSDK = sharedPreferences.getString(BizportContentUtil.ONLINE_UPDATE_SDK, SettingSDKBean.ONLINE_UPDARA_SDK_YES);
//        String smartsmsEnchance = sharedPreferences.getString(BizportContentUtil.SMARTSMS_ENCHANCE, Boolean.TRUE.toString());
//        String supportNetWorkType = sharedPreferences.getString(BizportContentUtil.SUPPORT_NETWORK_TYPE, SettingSDKBean.SUPPORT_NETWORK_TYPE_ALL);
//        String customPubInfoServerUrl = sharedPreferences.getString(BizportContentUtil.CUSTOM_PUBINFO_SERVER_URL, "");
//        String customLogoDownUrl = sharedPreferences.getString(BizportContentUtil.CUSTOM_LOGO_DOWN_URL, "");
//        boolean supportAidl = sharedPreferences.getBoolean(BizportContentUtil.SUPPORT_AIDL, false);
//
////        HashMap<String, String> extend = new HashMap<String, String>();
//        ConcurrentHashMap<String,String> extend = new ConcurrentHashMap<>();
//        //是否支持在线升级sdk 0:不支持 1:支持(默认)
//        extend.put(Constant.ONLINE_UPDATE_SDK, onlineUpdateSDK);
//        //增强开关 是否允许网络请求
//        extend.put(Constant.SMARTSMS_ENHANCE, smartsmsEnchance);
//        //支持的网络类型 不影响小流量请求 0:不支持 1:wifi 2:数据网络和wifi
//        extend.put(Constant.SUPPORT_NETWORK_TYPE, supportNetWorkType);
//
//        if (!TextUtils.isEmpty(customPubInfoServerUrl)) {
//            extend.put(Constant.CUSTOM_PUBINFO_SERVER_URL, customPubInfoServerUrl);
//        }
//        if (!TextUtils.isEmpty(customLogoDownUrl)) {
//            extend.put(Constant.CUSTOM_LOGO_DOWN_URL, customLogoDownUrl);
//        }
//
//        if (supportAidl) {
//            //设置是否需要使能远程识别 (不需要不写)
//            extend.put(SmsProcessFacade.KEY_REMOTE_PARSE_ENABLE, "true");
//            //设置提供远程服务的包名；默认为"com.xy.bizport"
//            extend.put(SmsProcessFacade.KEY_REMOTE_PARSE_PACKAGE, "com.xy.bizport");
//            //设置提供远程服务的ACTION名；默认为"com.xy.bizport.service.aidl.RemoteCallableStub"
//            extend.put(SmsProcessFacade.KEY_REMOTE_PARSE_ACTION, "com.xy.bizport.service.aidl.RemoteCallableStub");
//        }
//
//        //渠道密钥
//        extend.put(Constant.SECRETKEY, BuildConfig.DUOQU_SDK_CHANNEL_SECRETKEY);
//        //RSA渠道密钥
//        extend.put(Constant.RSAPRVKEY, BuildConfig.DUOQU_SDK_RSAKEY);
//        extend.put(Constant.CHANNEL, BuildConfig.DUOQU_SDK_CHANNEL);
//
//        SmsProcessFacade.getInstance(this).init(getSdkAction(), extend);
//        LogManager.debug = false;
    }

    /**
     * 初始化本机定位
     */
    private void initLocation() {
        //LocationManager.getInstance(this);
    }

    /**
     * 初始化本地数据的信息样例
     */
    private void initDB() {
        SmsDBManager.getInstance(this).registerContentObserver(new ContentObserver(mHandler) {
            @Override
            public boolean deliverSelfNotifications() {
                return super.deliverSelfNotifications();
            }

            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                Log.i(TAG, "onChange: ");
                SmsDBManager.getInstance(mContext).updateConversationFromMessage();
            }

            @Override
            public void onChange(boolean selfChange, Uri uri) {
                super.onChange(selfChange, uri);
            }
        });

    }

}
